//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2007
//
// 'Options' dialog, 'Google Maps' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "WebMap.h"
# include "MapView.h"
# include "Dialog.h"
# include "Settings.h"
# include "SelectFolder.h"
# include "Globals.h"

class CWMAdvanced : public CDialog {
  public:
	CWMAdvanced () : CDialog (IDD_OPTIONS_WEBMAP_ADVANCED) {}

	virtual bool OnInit (HWND _hwndFocus) {
		// Set cookie.
		CheckDlgButton (IDC_SET_COOKIE, g_bGMSetCookie);

		// Version in URL.
//		SetDlgItemUInt (IDC_VERSION, g_dwGMVersion);

		return true;
	}
	virtual void OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
		switch (_wCmd) {
			case IDOK:
				// Set cookie.
				g_bGMSetCookie = IsDlgButtonChecked (IDC_SET_COOKIE);
				SaveSetting ("GoogleMapsSetCookie", g_bGMSetCookie);

				// Version in URL.
//				g_dwGMVersion = GetDlgItemUInt (IDC_VERSION);
//				SaveSetting ("GoogleMapsVersion", g_dwGMVersion);

			case IDCANCEL:
				EndDialog (0);
				break;
		}
	}
};

static
void _EnableCtrls (HWND _hWnd, bool _bEnable) {
	::EnableWindow (::GetDlgItem (_hWnd, IDC_GM_CACHE),       _bEnable);
	::EnableWindow (::GetDlgItem (_hWnd, IDC_BROWSE),         _bEnable);
	::EnableWindow (::GetDlgItem (_hWnd, IDC_WRITE_GM_CACHE), _bEnable);

	::EnableWindow (::GetDlgItem (_hWnd, IDC_NAMES),          _bEnable);
	::EnableWindow (::GetDlgItem (_hWnd, IDC_NAMES_STD),      _bEnable);
	::EnableWindow (::GetDlgItem (_hWnd, IDC_NAMES_ES),       _bEnable);
}

INT_PTR CALLBACK _OptionsWebMapProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			// Use online.
			::CheckDlgButton (_hWnd, IDC_USE_GM_ONLINE, g_bUseGMOnline ? BST_CHECKED : BST_UNCHECKED);

			// Use cache.
			::CheckDlgButton (_hWnd, IDC_USE_GM_CACHE,  g_bUseGMCache  ? BST_CHECKED : BST_UNCHECKED);

			// Path,
			char strGMCachePath [MAX_PATH] = "";
			GetSetting_GoogleMapsCache (strGMCachePath);
			::SetDlgItemText (_hWnd, IDC_GM_CACHE, strGMCachePath);

			// Write cache.
			::CheckDlgButton (_hWnd, IDC_WRITE_GM_CACHE, g_bWriteGMCache ? BST_CHECKED : BST_UNCHECKED);

			if (g_bWriteEarthSlicerNames)
				::CheckRadioButton (_hWnd, IDC_NAMES_ES,  IDC_NAMES_ES,  IDC_NAMES_ES);
			else
				::CheckRadioButton (_hWnd, IDC_NAMES_STD, IDC_NAMES_STD, IDC_NAMES_STD);

			_EnableCtrls (_hWnd, g_bUseGMCache);

			return TRUE;
		}

		case WM_COMMAND:
			switch (LOWORD (_wParam)) {
				case IDC_USE_GM_ONLINE:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_USE_GM_CACHE: {
					const bool bUseGMCache = ::IsDlgButtonChecked (_hWnd, IDC_USE_GM_CACHE) == BST_CHECKED;
					_EnableCtrls (_hWnd, bUseGMCache);
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;
				}

				case IDC_WRITE_GM_CACHE:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_BROWSE: {
					char strGMCachePath [MAX_PATH] = "";
					::GetDlgItemText (_hWnd, IDC_GM_CACHE, strGMCachePath, MAX_PATH);
					if (SelectFolder (_hWnd, strGMCachePath)) {
						::SetDlgItemText (_hWnd, IDC_GM_CACHE, strGMCachePath);
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					}
					break;
				}

				case IDC_GM_CACHE:
					if (HIWORD (_wParam) == EN_CHANGE)
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_NAMES_STD:
				case IDC_NAMES_ES:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					break;

				case IDC_ADVANCED: {
					CWMAdvanced ad;
					ad.ShowModal (_hWnd);
					break;
				}
			}

			return TRUE;

		case WM_NOTIFY: {
            const NMHDR * const pInfo = reinterpret_cast<NMHDR *> (_lParam);
            switch (pInfo->code) {
				case PSN_APPLY: {
					// Use online.
					g_bUseGMOnline = ::IsDlgButtonChecked (_hWnd, IDC_USE_GM_ONLINE) == BST_CHECKED;
					SaveSetting ("UseGoogleMapsOnline", g_bUseGMOnline);

					// Use cache.
					g_bUseGMCache = ::IsDlgButtonChecked (_hWnd, IDC_USE_GM_CACHE) == BST_CHECKED;
					SaveSetting ("UseGoogleMapsCache", g_bUseGMCache);

					// Path.
					char strGMCachePath [MAX_PATH] = "";
					::GetDlgItemText (_hWnd, IDC_GM_CACHE, strGMCachePath, MAX_PATH);
					SaveSetting ("GoogleMapsCache", strGMCachePath);

					// Write cache.
					g_bWriteGMCache = ::IsDlgButtonChecked (_hWnd, IDC_WRITE_GM_CACHE) == BST_CHECKED;
					SaveSetting ("WriteGoogleMapsCache", g_bWriteGMCache);

					// Name style.
					g_bWriteEarthSlicerNames = ::IsDlgButtonChecked (_hWnd, IDC_NAMES_ES) == BST_CHECKED;
					SaveSetting ("WriteEarthSlicerNames", g_bWriteEarthSlicerNames);

					Refresh ();

					break;
				}
			}

			break;
		}
	}

	return FALSE;
}
